home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** StandardFileIcons: A sample control panel changing behavior of StandardFile.
- **
- ** by Brian Bechtel, Apple Developer Technical Support
- **
- ** File: SFIcons.c
- **
- ** Copyright © 1994 Apple Computer, Inc.
- ** All rights reserved.
- **
- ** You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "DTS Sample Code"
- ** after having made changes. If you're going to re-distribute the source,
- ** we require that you make it clear in the source that the code was
- ** descended from Apple Sample Code, but that you've made changes.
- **
- ** Change history (most recent first)
- **
- ** <1> 940811 BL°B Check for System 7. Display failure icon and do
- ** nothing else if earlier than System 7. Macintosh
- ** Easy Open is the first product which actually
- ** provided the desktop icon feature; it can work all
- ** the way back to System 7, potentially.
- ** <0> 940802 BL°B First implementation
- **/
-
-
- #include <types.h>
- #include <memory.h>
- #include <events.h>
- #include <dialogs.h>
- #include <controls.h>
- #include <files.h>
- #include <Devices.h>
- #include <GestaltEqu.h>
- #include <Folders.h>
- #include <Errors.h>
- #include <Script.h>
- #include <ToolUtils.h>
- #include <Resources.h>
-
- #include "GestaltValue.h"
-
- enum {
- gestaltStandardFileUseGenericIcons = 3,
- kSFIconsPrefFileName = 128
- };
-
- pascal Handle main(short, short, short, short, EventRecord *, Handle, DialogPtr);
- Handle InitStorage(void);
- void DisposeStorage(Handle);
- void SetButton(short, DialogPtr, short);
- void DoHit(short, short, DialogPtr, Handle);
- void Generic(Handle);
- void CustomIcons(Handle);
- void ReportStatus(short, DialogPtr);
- Boolean UseGenericIcons(void);
- void CreatePreferenceFile(Handle);
- void DeletePreferenceFile(Handle);
- Boolean OKSystem(void);
-
- /*
- * This dispatcher routine must be the first code, and it needs to follow the Pascal
- * parameter passine mechanisms.
- */
-
- pascal Handle main(short message,
- short item,
- short numItems,
- short CPanelID,
- EventRecord *theEvent,
- Handle cdevStorage,
- DialogPtr CPDialog)
- {
- #pragma unused(CPanelID, theEvent)
-
- if (message == macDev) { /* We should never get this message, but if we do… */
- if (!OKSystem())
- return (Handle)0;
- else
- return (Handle)1; /* …always show up in the Control Panel unless pre-System 7. */
- } else {
- if (cdevStorage) { /* cdevStorage = 0 if we have an error -- ignore events */
- switch (message) { /* What are we supposed to do this call? */
- case initDev: /* Take care of initialization */
- cdevStorage = InitStorage();
- if (cdevStorage) { /* Allocate our storage */
- ReportStatus(numItems, CPDialog); /* Tell what the disk is now. */
- }
- break;
- case closeDev: /* Free the storage that we used */
- DisposeStorage(cdevStorage);
- break;
- case hitDev: /* The user clicked in the CP */
- HLock(cdevStorage);
- DoHit(item, numItems, CPDialog,cdevStorage);
- HUnlock(cdevStorage);
- break;
- }
- return cdevStorage; /* Return the cdevStorage. CP will put up alert if 0 */
- }
- }
- }
-
-
- Handle InitStorage(void)
- {
- return (Handle)GetString(kSFIconsPrefFileName);
- }
-
-
- void DisposeStorage(Handle h)
- {
- ReleaseResource(h);
- }
-
-
- void SetButton(short item, DialogPtr dialog, short value)
- {
- short itemType;
- Handle itemHandle;
- Rect itemBox;
-
- GetDItem(dialog, item, &itemType, &itemHandle, &itemBox); /* Get the ControlHandle */
- SetCtlValue((ControlHandle)itemHandle, value); /* And set the button value */
- }
-
-
- void DoHit(short item, short numItems, DialogPtr CPDialog, Handle preferenceFileName)
- {
- short i;
-
- for (i = 1; i <= 2; i++) { /* Do each of our 2 radio buttons */
- if (i == (item - numItems)) { /* Is this the button clicked in? */
- SetButton(i+numItems, CPDialog, 1); /* Yes. Turn it on. */
- if (i == 2)
- Generic(preferenceFileName); /* use generic icons. */
- else
- CustomIcons(preferenceFileName);
- } else {
- SetButton(i+numItems, CPDialog, 0); /* Nope. Turn others off. */
- }
- }
- }
-
-
- /*
- * Generic
- *
- * Set the Gestalt bit in gestaltStandardFileAttr which says to use Generic Icons
- * Create a preference file. The INIT in this control panel will look for the
- * preference file in order to decide what behavior to set at startup time.
- */
- void Generic(Handle preferenceFileName)
- {
- long gestaltValue;
- OSErr err;
-
- err = Gestalt(gestaltStandardFileAttr, &gestaltValue);
- if (!err)
- {
- gestaltValue |= (1 << gestaltStandardFileUseGenericIcons);
- ReplaceGestaltValue(gestaltStandardFileAttr, gestaltValue);
- CreatePreferenceFile(preferenceFileName);
- }
- }
-
-
-
- /*
- * CustomIcons
- *
- * Clear the Gestalt bit in gestaltStandardFileAttr which says to use Generic Icons
- * Delete a preference file if it exists. The INIT in this control panel will look
- * for the preference file in order to decide what behavior to set at startup time.
- */
- void CustomIcons(Handle preferenceFileName)
- {
- long gestaltValue;
- OSErr err;
-
- err = Gestalt(gestaltStandardFileAttr, &gestaltValue);
- if (!err)
- {
- gestaltValue &= ~(1 << gestaltStandardFileUseGenericIcons);
- ReplaceGestaltValue(gestaltStandardFileAttr, gestaltValue);
- DeletePreferenceFile(preferenceFileName);
- }
- }
-
-
- void ReportStatus(short numItems, DialogPtr CPDialog)
- {
- if (UseGenericIcons()) {
- SetButton(2+numItems, CPDialog, 1); /* Set the 'black and white' button */
- SetButton(1+numItems, CPDialog, 0); /* Turn off 'color' button */
- } else {
- SetButton(1+numItems, CPDialog, 1); /* Set the 'color' button */
- SetButton(2+numItems, CPDialog, 0); /* Turn off 'black and white' button */
- }
- }
-
-
- /* Decide to use generic icons or not. */
- Boolean UseGenericIcons(void)
- {
- long response;
- OSErr err;
-
- err = Gestalt(gestaltStandardFileAttr, &response);
- if (!err)
- {
- return(response & (1 << gestaltStandardFileUseGenericIcons));
- }
- else
- return false;
- }
-
-
- /* Create our preference file in the Preference folder */
- void CreatePreferenceFile(Handle preferenceFileName)
- {
- OSErr err;
- FSSpec spec;
- short foundVRefNum;
- long foundDirID;
-
- err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&foundVRefNum,&foundDirID);
- if (!err)
- err = FSMakeFSSpec(foundVRefNum,foundDirID,(StringPtr)*preferenceFileName,&spec);
- if ((err == noErr) || (err == fnfErr))
- err = FSpCreate(&spec,'SFI#','PREF',smCurrentScript);
- }
-
-
- /* Delete our preference file in the preference folder */
- void DeletePreferenceFile(Handle preferenceFileName)
- {
- OSErr err;
- FSSpec spec;
- short foundVRefNum;
- long foundDirID;
-
- err = FindFolder(kOnSystemDisk,kPreferencesFolderType,false,&foundVRefNum,&foundDirID);
- if (!err)
- err = FSMakeFSSpec(foundVRefNum,foundDirID,(StringPtr)*preferenceFileName,&spec);
- if (!err)
- err = FSpDelete(&spec);
- }
-
-
- /* Check System Version and return false if primary digit is < 7 (e.g. System 6)*/
- Boolean OKSystem(void)
- {
- long response;
- OSErr err;
-
- err = Gestalt(gestaltSystemVersion, &response);
-
- return ( (err == noErr) && ((response >> 8) >= 0x07) );
- }
-
-